<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基础表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
       
               
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                
                  <el-table-column property="commodityName" label="商品名称" />
                    <el-table-column property="commoditySalePrice" label="商品价格"  />
                         <el-table-column property="commodityDetails" label="商品描述" />
                      <el-table-column property="commodityImg" label="商品图片">
          <template #default="sc">
            <div>
              <img :src="img + sc.row.commodityImg" width="120" height="200" />
            </div>
          </template>
        </el-table-column>
                         <el-table-column property="commentCount" label="点赞评论数" />
                    <el-table-column property="commoditySaleNum" label="商品销量"  />
                   <el-table-column property="commodityBorwseNum" label="商品游览数"  />

                
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="Del(scope.$index, scope.row)"
                            
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
              <div class="block">
    
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="index"
      :page-sizes="[1, 3, 5, 7]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="Count">
    </el-pagination>
  </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
               <el-form-item label="商品名称">
                        <el-input v-model="form.commodityName"></el-input>
                    </el-form-item>
                      <el-form-item label="商品价格">
                        <el-input v-model="form.commoditySalePrice"></el-input>
                    </el-form-item>
                        <el-form-item label="商品描述">
                        <el-input v-model="form.commodityDetails"></el-input>
                    </el-form-item>
                     <el-form-item label="商品图片">
                        <el-input v-model="form.commodityImg"></el-input>
                    </el-form-item>
                    
                        <el-form-item label="点赞评论数">
                        <el-input v-model="form.commentCount"></el-input>
                    </el-form-item>
                        <el-form-item label="商品销量">
                        <el-input v-model="form.commoditySaleNum"></el-input>
                    </el-form-item>
                        <el-form-item label="商品游览量">
                        <el-input v-model="form.commodityBorwseNum"></el-input>
                    </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>


</template>

<script>
import axios from 'axios'
import { fetchData } from "../api/index";
export default {
    name: "basetable",
    data() {
        return {
            query: {
                address: "",
                name: "",
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {
           commodityName:'',
commoditySalePrice:'',
commodityDetails:'',
commodityImg:'',
commentCount:'',
commoditySaleNum:'',
commodityBorwseNum:'',

            },
            img:"http://qiniuimg2.tablewareart.net/",
            index:0,
            size:3,
            Count:0
        };
    },
   
    mounted()
    {
      this.Show();
   
    },

    methods: {
         handleSizeChange(size) {
        this.size=size;
        this.Show();
      },
      handleCurrentChange(index) {
       this.index=index;
       this.Show();
      },
      Show() {
         axios({
           method:"get",
           url:"https://localhost:44310/api/app/admin-comm",
           params:{SkipCount:this.index,MaxResultCount:this.size,Sorting:"id"}
         }).then((res)=>{
            this.tableData=res.data.items;
            this.Count=res.data.totalCount;
            
         })
      },
      // 获取 easy-mock 的模拟数据
      getData() {
        fetchData(this.query).then((res) => {
          console.log(res);
          this.tableData = res.list;
          this.pageTotal = res.pageTotal || 50;
        });
      },
      // 触发搜索按钮
      handleSearch() {
        this.$set(this.query, "pageIndex", 1);
        this.getData();
      },
      // 删除操作
      handleDelete(index) {
        // 二次确认删除
        this.$confirm("确定要删除吗？", "提示", {
          type: "warning",
        })
          .then(() => {
            this.$message.success("删除成功");
            this.tableData.splice(index, 1);
          })
          .catch(() => {});
      },
      // 多选操作
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      delAllSelection() {
        const length = this.multipleSelection.length;
        let str = "";
        this.delList = this.delList.concat(this.multipleSelection);
        for (let i = 0; i < length; i++) {
          str += this.multipleSelection[i].name + " ";
        }
        this.$message.error(`删除了${str}`);
        this.multipleSelection = [];
      },
      // 编辑操作
      handleEdit(index, row) {
        axios({
          url: "https://localhost:44310/api/Gfan?gid=" + row.id,
          method:"post",
        }).then((res) => {
          console.log(row);
          this.form = res.data;
          this.editVisible = true;
        });
      },
      // 保存编辑
      saveEdit() {
        
        axios({
          url: "https://localhost:44310/api/Gupdate",
          method:"put",
          data:this.form,
        }).then((res) => {
          
          if (res.data.code == 204) {
            alert("修改失败");
            this.$router.push({ name: "BaseTable" });
          } else {
            alert("修改成功");
          }
        });
      },
      // 分页导航
      handlePageChange(val) {
        this.$set(this.query, "pageIndex", val);
        this.getData();
      },
      Del(index, row) {
        // 二次确认删除
        if (confirm("确认删除吗")) {
          axios({
            method: "DELETE",
            url: "https://localhost:44310/api/Gdel?gu=" + row.id,
          }).then((res) => {
            if (res.data.code == 204) {
              alert("删除失败");
             
            } else {
              alert("删除成功");
               this.Show();
            }
          });
        }
      },
    },
}
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>